package com.dzl.fee.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.fee.dao.WelfareFeeOrderMapper;
import com.dzl.fee.pojo.WelfareFeeOrder;
import com.dzl.fee.pojo.WelfareFeeOrderDetail;
import com.dzl.fee.pojo.query.WelfareFeeOrderQuery;
import com.dzl.fee.pojo.vo.WelfareFeeOrderVo;
import com.dzl.fee.pojo.vo.WelfareFeeSalesCustomersNameVo;
import com.dzl.fee.service.WelfareFeeOrderDetailService;
import com.dzl.fee.service.WelfareFeeOrderService;
import com.dzl.util.EmptyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务员订单表(WelfareFeeOrder)表服务实现类
 *
 * @author wangjian
 * @date 2021-08-03 15:23:39
 */
@Service
public class WelfareFeeOrderServiceImpl extends BaseServiceImpl<WelfareFeeOrder> implements WelfareFeeOrderService {

    @Autowired
    private WelfareFeeOrderMapper mapper;
    @Autowired
    private WelfareFeeOrderDetailService welfareFeeOrderDetailService;

    @Override
    public PageInfo<WelfareFeeOrderVo> getFeeOrderListPage(WelfareFeeOrderQuery query) {
        PageHelper.startPage(query);
        List<WelfareFeeOrderVo> list = mapper.getFeeOrderList(query);
        PageInfo<WelfareFeeOrderVo> page = new PageInfo<WelfareFeeOrderVo>(list);
        return page;
    }

    @Override
    public WelfareFeeOrderVo getFeeOrderDetail(Integer id) {
        WelfareFeeOrderQuery welfareFeeOrderQuery = new WelfareFeeOrderQuery();
        WelfareFeeOrderVo welfareFeeOrderVo = new WelfareFeeOrderVo();
        welfareFeeOrderQuery.setId(id);
        List<WelfareFeeOrderVo> feeOrderList = getFeeOrderListPage(welfareFeeOrderQuery).getList();
        if (EmptyUtils.isNotEmpty(feeOrderList)){
            welfareFeeOrderVo = feeOrderList.get(0);
        }
        WelfareFeeOrderDetail welfareFeeOrderDetail = new WelfareFeeOrderDetail();
        welfareFeeOrderDetail.setOrderId(id);
        welfareFeeOrderDetail.setStatus(1);
        List<WelfareFeeOrderDetail> orderDetailList = welfareFeeOrderDetailService.select(welfareFeeOrderDetail);
        welfareFeeOrderVo.setWelfareFeeOrderDetailList(orderDetailList);
        return welfareFeeOrderVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateOrderDetail(WelfareFeeOrderVo vo) {
        //修改订单总金额、回款金额和未回款金额
        mapper.updateAmount(vo);
        //修改回款记录
        //1，删除原有记录
        WelfareFeeOrderDetail detail = new WelfareFeeOrderDetail();
        detail.setOrderId(vo.getId());
        welfareFeeOrderDetailService.delete(detail);
        //2，新增记录
        if (EmptyUtils.isNotEmpty(vo.getWelfareFeeOrderDetailList())){
            welfareFeeOrderDetailService.insertList(vo.getWelfareFeeOrderDetailList());
        }
        return 1;
    }

    @Override
    public Integer updateAmount(WelfareFeeOrderVo vo) {
        return mapper.updateAmount(vo);
    }

    @Override
    public WelfareFeeSalesCustomersNameVo selectSalesCustomersNameVoByFeeOrderId(Integer feeOrderId) {
        return mapper.selectSalesCustomersNameVoByFeeOrderId(feeOrderId);
    }
}